/*!
 * New Launch.
 * Date: 2015/12/29.
 * Copyright© 2014-2015 Vamaker All Rights Reserved.
 */
require([
    'jquery',
		'pageInit',
		'formValidator',
    'formAjax',
    'notification',
    'select2'
	], function ($, vamakerPage, formValidator, formAjax, VamOpMsg, select2) {
  $(function () {
    var $dataFrm = $('#data-frm'),
        $submitBtn = $dataFrm.find('button[type="submit"]'),
        dataFieldRule, options, dataValidator;
      $('#bind_users').select2();
      dataFieldRule = {
        advertiser_name:{
          rule:{
            required:{
              message: VAM.j('The brand name cannot be empty')
            }
          },
          messageContainer:'advertiser_name_err'
        },
        recharge_amount: {
          rule: {
            floatnumber:{
              message: VAM.j('Only float number')
            },
            minval:{
              param: parseFloat('0.01'),
              message: VAM.j('Enter a mininum of 0.01')
            },
            effectiveNumber:{
              message: VAM.j('Keep two decimal')
            }
          },
          messageContainer: 'recharge_amount_err'
        },
        website_name: {
          rule: {
            required: {
              message: VAM.j('Website name cannot be empty')
            }
          },
          messageContainer: 'website_name_err'
        },
        website_url:{
          rule:{
              required:{
                message: VAM.j('Website address cannot be empty')
              },
              url:{
                message: VAM.j('Please input the correct web address (starting with http:// or https://)')
              }
          },
          messageContainer: 'website_url_err'
        },
        website_contacter: {
          rule: {
            required: {
              message: VAM.j('The contact cannot be empty'),
            },
            sspname: {
              message: VAM.j('Only for Chinese, letters, Numbers, underscore, in line')
            }
          },
          messageContainer: 'website_contacter_err'
        },
        email_address: {
          rule: {
            required: {
              message: VAM.j('Email cannot be empty')
            },
            email: {
              message: VAM.j('Please enter the correct email')
            }
          },
          messageContainer: 'email_address_err'
        },
        cellphone_number: {
          rule:{
              required:{
                  message: VAM.j('Mobile phone number cannot be empty')
              },
              cellphone:{
                  message: VAM.j('Please enter the correct phone number')
              }
          },
          messageContainer: 'cellphone_number_err'
        },
        telephone_number: {
          rule: {
            required: {
              message: VAM.j('Telephone can not be empty')
            },
            fixedtelephone:{
              message: VAM.j('Please enter the correct format (area code) - (phone number)')
            }
          },
          messageContainer: 'telephone_number_err'
        }
        // bind_users:{
        //   rule:{
        //     required:{
        //       message: VAM.j('The target user can not be empty')
        //     }
        //   },
        //   messageContainer:'bind_users_err'
        // }
      };
      options = {
        submitOnSucceed: false,
        onvalidate: function(evt) {
          if (evt.resultList.length <= 0) {
              $dataFrm.ajaxSubmit({
                  beforeSubmit: function() {
                    $submitBtn.button('loading');
                  },
                  success: function (data) {
                    $submitBtn.button('reset');
                    if (data.status === 'success') {
                      VamOpMsg.messager(data);
                      window.location.href = 'brand-list';
                    } else {
                      VamOpMsg.messager(data);
                    }
                  }
              });
          };
        }
      };
      formValidator.validate($dataFrm, dataFieldRule, options);
      dataValidator = formValidator.dataValidator;
      dataValidator.addRule('effectiveNumber',function(val, opt){
        return !!(val && /^\d+(\.\d{1,2})?$/.test(val));
      });
  });
  //Creative Vocation.
  $(function(){
    var creativeVocation = $.parseJSON(VAM.getVar('CREATIVE_VOCATION')),
      $vocationLarge = $('#creative_vocation_large'),
      $vocationSmall = $('#creative_vocation_small');
    for(var key in creativeVocation){
      if(creativeVocation.hasOwnProperty(key)) {
        $vocationLarge.append('<option value="'+key+'">' + key + '</option>');
      }
    }

    $vocationLarge.on('change',function(){
      var $me = $(this),
        vocationSmall = creativeVocation[$me.val()],
        i,j;
      $vocationSmall.children().remove();
      $vocationSmall.append('<option value="' + VAM.j('All') + '">' + VAM.j('All') + '</option>');
      for (i=0, j=vocationSmall.length; i<j; i++) {
        $vocationSmall.append('<option value="' + vocationSmall[i] + '">' + vocationSmall[i] +'</option>');
      }
    });
    // $vocationLarge.trigger('change');
    
    var creativeParentVocation = VAM.getVar('parent_category');
    var creativeChildVocation = VAM.getVar('child_category');

    $("#creative_vocation_large option[value='"+creativeParentVocation+"']").attr('selected', "selected");
    $vocationLarge.trigger('change');
    $("#creative_vocation_small option[value='"+creativeChildVocation+"']").attr('selected', "selected");
  })
});
